<template>
  <div class="comment">
    <div class="addcomment" v-show="!isFocus">
      <input type="text" placeholder="写跟帖" @focus="handlerFocus" />
      <span class="comment">
        <i class="iconfont iconpinglun-"></i>
        <em>{{ obj.comment_length }}</em>
      </span>
      <i
        :class="{ active: this.obj.has_star }"
        class="iconfont iconshoucang"
        @click="postStarFn"
      ></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment" v-show="isFocus">
      <textarea ref="commtext" rows="5"></textarea>
      <div>
        <span @click="publishFn">发 送</span>
        <span @click="cancelFn">取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
import { postStar, postComment } from "@/apis/get.js";
export default {
  data() {
    return {
      isFocus: false,
    };
  },
  props: ["obj", "parent_id"],
  methods: {
    // 收藏文章
    postStarFn() {
      postStar(this.obj.id).then((res) => {
        // console.log(res);
        // console.log(40, this.obj);
        this.$toast(res.data.message);
        this.obj.has_star = !this.obj.has_star;
      });
    },
    // 聚焦
    handlerFocus() {
      this.$router.push({
        path: "comment",
        query: {
          id: this.obj.id,
        },
      });
      this.isFocus = true;
      // console.log(this.$refs.commtext);
      this.$nextTick(() => {
        this.$refs.commtext.focus();
      });
    },
    // 取消发布
    cancelFn() {
      this.$nextTick(() => {
        this.$refs.commtext.value = "";
        this.isFocus = false;
        this.parent_id = "";
      });
    },
    // 发布评论
    publishFn() {
      this.$nextTick(() => {
        let text = this.$refs.commtext.value;
        postComment(this.obj.id, {
          content: text,
          parent_id: this.parent_id,
        }).then((res) => {
          // console.log(67, res, text, this.obj.user.id);
          // console.log(this.obj);
          this.isFocus = false;
          this.$toast(res.data.message);
          this.$refs.commtext.value = "";
          this.$emit("updateCommentList");
          window.scrollTo(0, 0);
          this.parent_id = "";
          // console.log(93, this.parent_id);
        });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.comment {
  position: fixed;
  display: flex;
  bottom: 0;
  left: 0;
  width: 100%;
  margin-bottom: 30px;
}
.active {
  color: red;
}
.inputcomment {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  background-color: #fff;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 38px;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
</style>